package models;

import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import utils.ContainGame;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name="game")
public class Game extends Model implements ContainGame {

    @Id
    public long id;

    @ManyToOne
    public String account_email;

    public long idgamedb;

    @Constraints.Required
    public String gametitle;

    @Constraints.Required
    @Temporal(TemporalType.DATE)
    public Date releasedate;

    public String youtube;

    public short rating;

    @Constraints.Required
    @ManyToOne
    public Platform platform;

    @ManyToMany
    public List<Genre> genres = new ArrayList<>();

    @ManyToOne
    public Compagny publisher;

    @ManyToOne
    public Compagny developer;

    @OneToMany(mappedBy="game")
    public List<UserWishGame> userwishgames;

    public Game(String gameTitle) {
        if (gameTitle.equals(""))
            throw new IllegalArgumentException("Le nom du jeu est obligatoire.");
        this.gametitle = gameTitle;
    }

    // -- Queries

    public static Model.Finder<Long,Game> find = new Model.Finder(Long.class, Game.class);

    public static List<Game> findAll() {
        return find.fetch("developer").fetch("publisher").orderBy("gametitle").findList();
    }

    public static Game findById(Long id) {
        return find.fetch("developer").fetch("publisher").where().eq("id", id).findUnique();
    }

    public static Game findByIdGameDb(Long idGameDb) {
        return find.fetch("developer").fetch("publisher").where().eq("idgamedb", idGameDb).findUnique();
    }

    public static List<Game> findGameWithoutState(String email) {
        List<Game> result = findAll();
        result.removeAll(find.fetch("userwishgames").where().eq("userwishgames.account.email", email).findList());
        return result;
    }

    @Override
    public Game getGame() { return this; }

    // --

}
